<?php

namespace App\Http\Validate\Kms\Task;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Kms\App\ClientsModel;
use App\Models\Kms\Task\DailyTaskLogsModel;
use App\Models\Kms\Task\TaskModel;
use App\Models\Kms\Task\TaskNodesModel;
use App\Models\Kms\Task\TaskNodesProcessorModel;
use App\Service\Kms\Task\UserRoleService;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class TaskValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }


    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function shopListValidate()
    {
        return self::getInstance()->validate(
            ["role", "province"],
            request()->all(),
            [
                "role"     => "required|int|in:" . implode(",", [
                        UserRoleService::ROLE_BASE_PROVINCE,
                        UserRoleService::ROLE_BASE_SUPERVISOR,
                        UserRoleService::ROLE_BASE_CITY_MANAGER,
                        UserRoleService::ROLE_BASE_WAR_REGION,
                        UserRoleService::ROLE_BASE_DEVELOP_CITY_MANAGER,
                    ]),
                "province" => Rule::when(
                    attributeGet(request()->all(), "role", null) == UserRoleService::ROLE_BASE_SUPERVISOR,
                    [
                    ],
                    [
                        "required",
                        "string"
                    ]
                )
            ]
        );
    }

    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function supervisorListValidate()
    {
        return self::getInstance()->validate(
            ["shopInfo"],
            request()->all(),
            [
                "shopInfo"            => [
                    "required",
                    "array",
                    "min:1"
                ],
                "shopInfo.*.shopCode" => [
                    "required",
                    "int"
                ],
                "shopInfo.*.province" => [
                    "required",
                    "int"
                ],
            ]
        );
    }


    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function addValidate()
    {
        return self::getInstance()->validate(
            ["shopMap", "taskType", "desc", "descImg"],
            request()->all(),
            [
                "shopMap"                => [
                    "required",
                    "array",
                    "min:1"
                ],
                "shopMap.*.id"           => [
                    "required",
                    "int"
                ],
                "shopMap.*.shopCode"     => [
                    "required",
                    "int"
                ],
                "shopMap.*.province"     => [
                    "required",
                    "int"
                ],
                "shopMap.*.city"         => [
                    "required",
                    "int"
                ],
                "shopMap.*.district"     => [
                    "required",
                    "int"
                ],
                "shopMap.*.handleUserId" => [
                    "required",
                    "int"
                ],
                "taskType"               => self::intInValidateBuild(true, DailyTaskLogsModel::TASK_TYPE_TEXT),
                "desc"                   => "required|string",
                "descImg"                => "nullable|array|min:1",
                "descImg.*.type"         => "required|string|in:video,image",
                "descImg.*.url"          => "required|string|url"
            ]
        );
    }


    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function detailValidate()
    {
        return self::getInstance()->validate(
            ["id"],
            request()->all(),
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new DailyTaskLogsModel())->getTable(), "id")
                ]
            ]
        );
    }

    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function submitValidate()
    {
        return self::getInstance()->validate(
            ["id", "form"],
            request()->all(),
            [
                "id"               => [
                    "required",
                    "int",
                    Rule::exists((new DailyTaskLogsModel())->getTable(), "id"),
                    Rule::exists((new TaskModel())->getTable(), "businessId")->where(function ($query) {
                        $query->where("status", ">", TaskModel::TASK_STATUS_INVALID);
                    })
                ],
                "form"             => "required|array",
                "form.resultDesc"  => "required|string|min:1",
                "form.file"        => "required|array|min:1",
                "form.file.*.type" => "required|string|in:video,image",
                "form.file.*.url"  => "required|string|url"
            ]
        );
    }

    /**
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function invalidValidate()
    {
        return self::getInstance()->validate(
            ["id"],
            request()->all(),
            [
                "id" => [
                    "required",
                    "int",
                    Rule::exists((new DailyTaskLogsModel())->getTable(), "id"),
                    Rule::exists((new TaskModel())->getTable(), "businessId")->where(function ($query) {
                        $query->where("status", TaskModel::TASK_STATUS_ING);
                    })
                ]
            ]
        );
    }

    /**
     * 审批
     *
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function approvalValidate()
    {
        return self::getInstance()->validate(
            ["id", "approvalResult", "remark"],
            request()->all(),
            [
                "id"             => [
                    "required",
                    "int",
                    Rule::exists((new DailyTaskLogsModel())->getTable(), "id"),
                    Rule::exists((new TaskModel())->getTable(), "businessId")->where(function ($query) {
                        $query->where("status", ">", TaskModel::TASK_STATUS_INVALID);
                    })
                ],
                "approvalResult" => self::intInValidateBuild(true, TaskNodesModel::STATUS_SELECTION),
                "remark"         => [
                    "exclude_unless:approvalResult," . TaskNodesModel::BUSINESS_STATUS_REJECT,
                    "required",
                    "string",
                    "min:1"
                ]
            ]
        );
    }
}
